package com.example.myys

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.media3.common.util.UnstableApi
import coil3.ImageLoader
import coil3.SingletonImageLoader
import coil3.network.okhttp.OkHttpNetworkFetcherFactory
import coil3.request.crossfade
import com.example.myys.Constants.Companion.darkModeState
import com.example.myys.data.repository.SettingLinkRepository
import com.example.myys.network.HttpClient
import com.example.myys.network.parse.YsDataSource
import com.example.myys.ui.page.MyApp
import com.example.myys.ui.theme.MyYsTheme

class MainActivity : ComponentActivity() {
    private var exitTime: Long = 0 //双击退出记录

    @androidx.annotation.OptIn(UnstableApi::class)
    @OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        //初始化数据源对象
        YsDataSource.printValue()
        YsDataSource.someValue = "测试"
        YsDataSource.printValue()
        //初始化配置
        SettingLinkRepository.initSetting()
        SingletonImageLoader.setSafe { context ->
            ImageLoader.Builder(this)
                .crossfade(true) // 可选：淡入动画
                .components {
                    add(OkHttpNetworkFetcherFactory(callFactory = { HttpClient.client!! }))
                }
                .build()
        }
        setContent {
            val windowSizeClass = calculateWindowSizeClass(this)

            MyYsTheme(darkTheme = darkModeState.value) {
                MyApp(windowSize = windowSizeClass, this)
            }


            /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                //判断是否有管理外部存储的权限
                if (!Environment.isExternalStorageManager()) {
                    //TODO 跳转到权限页，请求权限
                    // 我们没有被授予权限，所以我们应该去请求它
                    ActivityCompat.requestPermissions(this,
                        arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.MANAGE_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE),
                        1);
                }
            }*/
        }
    }
}
